package com.abing.starry.controller;

import com.abing.starry.annotation.AuthCheck;
import com.abing.starry.common.BaseController;
import com.abing.starry.common.ErrorCode;
import com.abing.starry.exception.ThrowUtils;
import com.abing.starry.model.entity.UserEntity;
import com.abing.starry.model.request.user.OpsUserRequest;
import com.abing.starry.model.request.user.QueryUserRequest;
import com.abing.starry.model.request.user.UserLoginRequest;
import com.abing.starry.model.vo.LoginCaptchaVO;
import com.abing.starry.service.UserService;
import com.abing.starry.strategy.login.LoginStrategyExecutor;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ContentDisposition;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author CaptainBing
 * @Date 2023/11/8 22:10
 * @Description
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController implements BaseController<UserEntity> {

    private final UserService userService;

    private final LoginStrategyExecutor loginStrategyExecutor;

    @Override
    public IService<UserEntity> getService() {
        return userService;
    }

    @PostMapping("/register")
    public boolean userRegister(@RequestBody UserLoginRequest userLoginRequest) {
        return userService.userRegister(userLoginRequest);
    }

    @PostMapping("/login")
    public String userLogin(@RequestBody UserLoginRequest userLoginRequest) {

        String loginType = userLoginRequest.getLoginType();
        ThrowUtils.throwIf(StringUtils.isEmpty(loginType), ErrorCode.PARAMS_ERROR, "登录类型不存在");

        return loginStrategyExecutor.doLogin(userLoginRequest);

    }

    @GetMapping("/logout")
    public boolean userLogout() {
        return userService.userLogout();
    }

    @GetMapping("/captcha")
    public boolean sendCaptcha(@RequestParam String userAccount) {
        return userService.sendCaptcha(userAccount);
    }

    @GetMapping("/login/captcha")
    public LoginCaptchaVO fetchCaptchaToBase64() {
        return userService.fetchCaptchaToBase64();
    }

    @GetMapping("/get")
    public UserEntity getCurrentUser() {
        return userService.getCurrentUser();
    }


    @PostMapping("/query/page")
    @AuthCheck(mustRole = "admin")
    public IPage<UserEntity> queryUserByPage(@RequestBody QueryUserRequest queryUserRequest) {
        return userService.queryUserByPage(queryUserRequest);
    }

    @PostMapping("/ops")
    @AuthCheck(mustRole = "admin")
    @Operation(summary = "封禁用户")
    public boolean opsUserById(@RequestBody OpsUserRequest opsUserRequest) {
        return userService.opsUserById(opsUserRequest);
    }

    @GetMapping("/export")
    @AuthCheck(mustRole = "admin")
    public void exportAllUser(HttpServletResponse response) {

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition", ContentDisposition.attachment()
                                                                    .filename("用户信息.xlsx", StandardCharsets.UTF_8)
                                                                    .build()
                                                                    .toString());

        try {
            EasyExcel.write(response.getOutputStream())
                     .excelType(ExcelTypeEnum.XLSX)
                     .sheet("用户信息")
                     .head(UserEntity.class)
                     .doWrite(userService.list());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
